package com.bsc.rtc.lib.libraries.MyViewDemo.View;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.OverScroller;
import android.widget.Scroller;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;


public class CloudRainView extends View {
    private static final String TAG = "CloudRainView";
    private Paint paint;
    private Path cloudPath;
    private Path rainPath;
    private int cloudLeft;
    private int cloudTop;
    private int cloudRight;
    private int cloudBottom;
    private float cloudRadiusX;
    private float cloudRadiusY;
    private int topLeftCloudX;
    private int topLeftCloudY;
    private int topLeftCloudRadius;
    private int topRightCloudX;
    private int topRightCloudY;
    private int topRightCloudRadius;
    Matrix matrix;
    List<Path> rainList;

    public CloudRainView(Context Context) {
        super(Context);
        init();
        initCloudPath();
    }

    public CloudRainView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        initCloudPath();
    }

    public CloudRainView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        initCloudPath();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(10);
        matrix = new Matrix();

        rainList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            rainList.add(new Path());
        }
    }

    private void changPaint(int style) {
        switch (style) {
            case 0:
                paint.setColor(Color.BLACK);
                paint.setStrokeWidth(10);
                break;
            case 1:
                paint.setPathEffect(new DashPathEffect(new float[]{4.0f, 4.0f}, 0));
                paint.setStrokeWidth(5);
                paint.setColor(Color.BLUE);
                paint.setStyle(Paint.Style.STROKE);
                break;
        }
    }

    private void initCloudPath() {
        cloudPath = new Path();
        rainPath = new Path();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);/*
        int withMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        switch (withMode) {
            case MeasureSpec.AT_MOST:
                break;
            case MeasureSpec.EXACTLY:
                break;
            case MeasureSpec.UNSPECIFIED:
                break;
        }
*/
        //测量完成初始话view
        initView();
    }

    private void initView() {
        Log.i(TAG, "width=" + getMeasuredHeight());
        changPaint(0);
        cloudLeft = getPaddingLeft();
        cloudTop = getMeasuredHeight() / 3 + getPaddingTop();
        cloudRight = getMeasuredWidth() - getPaddingRight();
        cloudBottom = getMeasuredHeight() / 3 * 2 - getPaddingBottom();
        cloudRadiusY = cloudTop + ((cloudBottom - cloudTop) / 2);
        cloudRadiusX = cloudLeft + ((cloudRight - cloudLeft) / 2);
        cloudPath.addRoundRect(cloudLeft, cloudTop, cloudRight, cloudBottom, cloudRadiusX, cloudRadiusY, Path.Direction.CW);
        topLeftCloudX = cloudLeft + ((cloudRight - cloudLeft) / 3);
        topLeftCloudY = cloudTop - 10;
        topLeftCloudRadius = cloudTop / 2;
        cloudPath.addCircle(topLeftCloudX, topLeftCloudY, topLeftCloudRadius, Path.Direction.CW);
        topRightCloudX = topLeftCloudX = cloudLeft + ((cloudRight - cloudLeft) / 3 * 2);
        topRightCloudY = cloudTop - 20;
        topRightCloudRadius = cloudTop / 5 * 3;
        cloudPath.addCircle(topRightCloudX, topRightCloudY, topRightCloudRadius, Path.Direction.CW);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //   moveToCenter(canvas);
        drawCloud(canvas);
        drawRainView(canvas);
    }

    private void moveToCenter(Canvas canvas) {
        Log.i(TAG, canvas.getWidth() + " " + canvas.getHeight());
        canvas.translate(canvas.getWidth() / 2, canvas.getHeight() / 2);
    }

    private void drawRainView(Canvas canvas) {
        changPaint(1);
        rainPath.moveTo(cloudLeft, cloudBottom);
        rainPath.lineTo(cloudLeft, getMeasuredHeight());
        for (Path path : rainList) {
            matrix.postTranslate(20, 0);
            rainPath.transform(matrix, path);
            canvas.drawPath(path, paint);
        }
    }

    private void drawCloud(Canvas c) {
        c.drawPath(cloudPath, paint);
    }

    private void startAnimation(){

    }
}
